package servicebase

import (
	"jkgo/jkbase"
	"time"
)

//
// Content of message with src and dst
// means msg send is src, will send message to dst
// with data Data

// MsgContent Content of normal TCP msg payload
type MsgContent struct {
	Cmd  string
	Src  string
	Dst  string
	Data interface{}
}

// MsgHeader MQTT data header
type MsgHeader struct {
	// who you are
	ID string
	// what you want to do
	Cmd string
	// sequence id
	Seq uint
	// Timestamp
	Timestamp int64 // time.Now().Unix()
}

// MsgPayload Mqtt Use
type MsgPayload struct {
	MsgHeader
	// animal
	// plant
	// flower
	// text
	// face ...
	ToType string // You want to check if it is what
	Data   string // base64 aiimage content
}

// JKSeqGet get sequence
func JKSeqGet(seq uint) uint {
	if seq >= jkbase.UIntMax-1 {
		seq = 0
	} else {
		seq++
	}
	return seq
}

func (mp *MsgPayload) Make(id, cmd string, seq uint, totype, data string) uint {
	mp.MsgHeader.ID = id
	mp.MsgHeader.Cmd = cmd
	mp.MsgHeader.Seq = JKSeqGet(seq)
	mp.MsgHeader.Timestamp = time.Now().UnixNano() / 1000000
	mp.ToType = totype
	mp.Data = data
	return mp.MsgHeader.Seq+1
}

func (mp *MsgPayload) Retime() {
	mp.MsgHeader.Timestamp = time.Now().UnixNano() / 1000000
}
